package com.the4man.cnbdw.utils;

import com.the4man.framework.utils.ParameterItem;
import org.apache.struts2.ServletActionContext;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
public class Page<T> {
    private String pageStr;
    private List<T> data;
    public static final String PAGE_PARAM_NAME = "curPage";
    public static final String PAGESIZE_PARAM_NAME = "rowsPerPage";
    private int curPage;
    private int rowsPerPage;
    private int rowCount;
    private List<ParameterItem> params;
    private String url;
    private String urlEncodingCharset;
    private BaseForm form;

    public String getUrlEncodingCharset() {
        return this.urlEncodingCharset;
    }

    public void setUrlEncodingCharset(String urlEncodingCharset) {
        this.urlEncodingCharset = urlEncodingCharset;
    }

    public List<T> getData() {
        return this.data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Page() {
        this.curPage = 1;
        this.rowsPerPage = 15;
        this.params = new ArrayList();
        this.urlEncodingCharset = "UTF-8";
        this.url = ServletActionContext.getRequest().getRequestURI();
    }

    public Page(List<T> data) {
        this();
        this.data = data;
        this.setCurPage(1);
        this.setRowsPerPage(data.size());
        this.setRowCount(data.size());
        this.url = ServletActionContext.getRequest().getRequestURI();
    }

    public Page(List<T> source, int curPage, int rowCountPerPage, int rowcount) {
        this();
        this.data = source;
        this.curPage = curPage;
        this.rowsPerPage = rowCountPerPage;
        this.rowCount = rowcount;
    }

    public Page(Object baseForm) {
        this();
        this.form = (BaseForm)baseForm;
    }

    public int getPageCount() {
        return this.getRowCount() % this.getRowsPerPage() == 0?this.getRowCount() / this.getRowsPerPage():this.getRowCount() / this.getRowsPerPage() + 1;
    }

    public int getRowsPerPage() {
        return this.rowsPerPage;
    }

    public void setRowsPerPage(int rowsPerPage) {
        this.rowsPerPage = rowsPerPage;
    }

    public void addParams(List<ParameterItem> params) {
        this.params.addAll(params);
    }

    public void addParam(ParameterItem param) {
        this.params.add(param);
    }

    public int getRowCount() {
        return this.rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getCurPage() {
        return this.curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }
}
